<?php
namespace app\admin\model;

use think\Db;
class ApiSpotModel extends CommonModel
{
    private static $model;

    public function __construct()
    {
        self::$model = Db::name('api_spot');
    }

    /**
     * func  更新 or 添加
     */
    public function addOrUpdateApiSpotInfo($data = [], $id = 0)
    {
        if (!$data) {
            return false;
        }
        if ($id) {
            $data = array_merge($data, ['update_time' => time()]);
            if (self::$model->where(array('id' => $id))->update($data)) {
                return $id;
            } else {
                return false;
            }
        } else {
            $data = array_merge($data, ['create_time' => time(), 'update_time' => time(), 'uuid' => uuid()]);
            if (self::$model->insert($data)) {
                return self::$model->getLastInsID();
            } else {
                return false;
            }
        }
    }

    /**
     * func 列表
     */
    public function getApiSpotListInfo($condition = [], $orderArr = [], $pageArr = [], $fieldsArr = [], $joinArr = [])
    {
        return self::$model->alias('a')
            ->field($fieldsArr)
            ->where($condition)
            ->join($joinArr)
            ->order($orderArr)
            ->paginate($pageArr['limit'], false, ['page' => $pageArr['page']]);
    }

    /**
     * func 单条
     */
    public function getApiSpotOneInfo($condition = [], $orderArr = [], $fieldsArr = [], $joinArr = [])
    {
        return self::$model->alias('a')
            ->field($fieldsArr)
            ->where($condition)
            ->join($joinArr)
            ->order($orderArr)
            ->find();
    }

    /**
     * func 总数
     */
    public function getApiSpotCount($condition = [])
    {
        return (int)self::$model->alias('a')
            ->where($condition)
            ->count();
    }

    /**
     * func 求和
     */
    public function getApiSpotSum($condition = [], $field = null)
    {
        return (float)self::$model->alias('a')
            ->where($condition)
            ->sum($field);
    }
}